- Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTest_with_Retraind_Modules.py
433 lines (359 loc) · 15.9 KB
/
Test_with_Retraind_Modules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
fromPyQt5.QtWidgetsimportQApplication , QFileDialog , QFrame,QComboBox, QLineEdit , QLabel, QMessageBox, QWidget, QPushButton
fromPyQt5.QtCoreimport*
fromPyQt5importQtGui
fromPyQt5importQtCore
fromPyQt5.QtGuiimportQPixmap
fromResult_Retraind_Window_FinalimportResultWindow
fromPILimportImage, ImageChops, ImageEnhance
fromkeras.modelsimportload_model
frompylabimport*
importos
classThread(QThread):
def__init__(self):
QThread.__init__(self)
def__del__(self):
self.wait()
deftest_image_with_ela(self, image_path, model_path):
"""
Error Level Analysis
:param image_path
:return label[Forged , Not Forged] , prob[class probability]
"""
# loading Model
model=load_model(model_path)
# Read image
image_saved_path=image_path.split('.')[0] +'.saved.jpg'
# calculate ELA
image=Image.open(image_path).convert('RGB')
image.save(image_saved_path, 'JPEG', quality=90)
saved_image=Image.open(image_saved_path)
ela=ImageChops.difference(image, saved_image)
extrema=ela.getextrema()
max_diff=max([ex[1] forexinextrema])
ifmax_diff==0:
max_diff=1
scale=255.0/max_diff
ela_im=ImageEnhance.Brightness(ela).enhance(scale)
# prepare image for testing
image=array(ela_im.resize((128, 128))).flatten() /255.0
image=image.reshape(-1, 128, 128, 3)
# prediction
prob=model.predict(image)[0]
idx=np.argmax(prob)
pred=model.predict(image)
pred=pred.argmax(axis=1)[0]
label="Forged"ifpred==1else"Not_Forged"
returnlabel, prob[idx]
deftest_image_with_vgg16(self , image_path,model_path):
"""
VGG16 GoogleNet Competition Pre-trained Model
:param image_path
:return label[Forged , Not Forged] , prob[class probability]
"""
model=load_model(model_path)
# Read image
image=Image.open(image_path).convert('RGB')
# prepare image for testing
image=array(image.resize((300, 300))).flatten() /255.0
image=image.reshape(-1, 300, 300, 3)
# Make predictions on the input image
prob=model.predict(image)[0]
idx=np.argmax(prob)
# predictions
prob=model.predict(image)[0]
idx=np.argmax(prob)
pred=model.predict(image)
pred=pred.argmax(axis=1)[0]
label="Forged"ifpred==1else"Not_Forged"
returnlabel, prob[idx]
deftest_image_with_vgg19(self , image_path,model_path):
"""
VGG19 GoogleNet Competition Pre-trained Model
:param image_path
:return label[Forged , Not Forged] , prob[class probability]
"""
model=load_model(model_path)
# Read image
image=Image.open(image_path).convert('RGB')
# prepare image for testing
image=array(image.resize((100, 100))).flatten() /255.0
image=image.reshape(-1, 100, 100, 3)
prob=model.predict(image)[0]
idx=np.argmax(prob)
prob=model.predict(image)[0]
idx=np.argmax(prob)
pred=model.predict(image)
pred=pred.argmax(axis=1)[0]
label="Forged"ifpred==1else"Not_Forged"
returnlabel, prob[idx]
classTest_window(QWidget):
def__init__(self, parent=None , model_name="ELA" , model_path=".\\sys_models\\ELA_Model.h5" , flag="here"):
super().__init__()
self.title="IFD Application"
self.top=200
self.left=500
self.width=550
self.height=345
self.file_path=""
self.model_path=model_path
self.model_name=model_name
self.model=""
self.flag=flag
self.init_window()
definit_window(self):
"""initialize window"""
self.setWindowTitle(self.title)
self.setWindowIcon(QtGui.QIcon("icons8-cbs-512.ico")) #icon Pic File name
self.setGeometry(self.left , self.top , self.width , self.height)
self.setFixedSize(self.width , self.height)
self.label=QLabel(self)
self.label2=QLabel(self)
self.label3=QLabel(self)
self.label4=QLabel(self)
self.label_1=QLabel(self)
self.label_2=QLabel(self)
#quit = QAction("Quit", self)
#quit.triggered.connect(self.closex)
#Label
label=QLabel(self)
label.move(10,44)
label.setText('Image Name ')
label.setFont(QtGui.QFont("Sanserif" , 8))
#text Box
self.line_edit=QLineEdit(self)
self.line_edit.setReadOnly(True)
self.line_edit.setFont(QtGui.QFont("Sanserif", 8))
self.line_edit.setGeometry(QRect(80, 40, 365, 20))
self.line_edit.setPlaceholderText("image Name here!")
#Button
self.button=QPushButton("Browse", self)
self.button.setGeometry(QRect(450, 40, 90, 20))
self.button.setToolTip("<h5>Browse image from your computer to start test!<h5>") # Notice using h2 tags From Html
self.button.setIcon(QtGui.QIcon("698831-icon-105-folder-add-512.png")) #icon Pic File name
self.button.setIconSize(QtCore.QSize(15, 15)) # to change icon Size
self.button.clicked.connect(self.getfiles)
#Button
self.button=QPushButton("Test", self)
self.button.setGeometry(QRect(270, 310, 90, 20))
self.button.setToolTip("<h5>test image either Forged or Not Forged!<h5>") # Notice using h2 tags From Html
self.button.setIcon(QtGui.QIcon("698827-icon-101-folder-search-512.png")) #icon Pic File name
self.button.setIconSize(QtCore.QSize(15, 15)) # to change icon Size
self.button.clicked.connect(self.on_click)
#Button
self.button=QPushButton("Back", self)
self.button.setGeometry(QRect(180, 310, 90, 20))
self.button.setToolTip("<h5>test image either Forged or Not Forged!<h5>") # Notice using h2 tags From Html
self.button.setIcon(QtGui.QIcon("repeat-pngrepo-com.png")) #icon Pic File name
self.button.setIconSize(QtCore.QSize(15, 15)) # to change icon Size
self.button.clicked.connect(self.back_to_Main)
#Button
self.button=QPushButton(" Quit", self)
self.button.setGeometry(QRect(360, 310, 90, 20))
self.button.setToolTip("<h5>Close the program!<h5>") # Notice using h2 tags From Html
self.button.setIcon(QtGui.QIcon("cancel-symbol-transparent-9.png")) #icon Pic File name
self.button.setIconSize(QtCore.QSize(15, 15)) # to change icon Size
self.button.clicked.connect(self.close_main_window)
#Button
#self.button = QPushButton("Help", self)
#self.button.setGeometry(QRect(450, 310, 90, 20))
#self.button.setToolTip("<h5>Help!<h5>") # Notice using h2 tags From Html
#self.button.setIcon(QtGui.QIcon("icons8-faq-100 (1).png")) #icon Pic File name
#self.button.setIconSize(QtCore.QSize(15, 15)) # to change icon Size
#self.button.clicked.connect(self.on_click_help)
label=QLabel(self)
label.setText('Model ')
label.setFont(QtGui.QFont("Sanserif", 8))
label.move(10, 20)
head, tail=os.path.split(self.model_name)
self.combo=QComboBox(self)
self.combo.addItem(tail)
self.combo.setGeometry(QRect(80, 15,460 , 20))
label=QLabel(self)
label.setText('Image Informations')
label.setFont(QtGui.QFont("Sanserif", 8))
label.move(50, 75)
topleft=QFrame(self)
topleft.setFrameShape(QFrame.StyledPanel)
topleft.setGeometry(QRect(10, 90,175 , 200))
label=QLabel(self)
label.setText('Image')
label.setFont(QtGui.QFont("Sanserif", 8))
label.move(290, 75)
topleft=QFrame(self)
topleft.setFrameShape(QFrame.StyledPanel)
topleft.setGeometry(QRect(200, 90,200 , 200))
self.show()
@pyqtSlot()
defback_to_Main(self):
fromMain_window_FinalimportMainWindow
self.Main_window=MainWindow()
self.Main_window.show()
self.close()
@pyqtSlot()
defgetfiles(self):
fileName, extention=QFileDialog.getOpenFileName(self, 'Single File', 'C:\'',"*.png *.xpm *.jpg *.tiff *.jpg *.bmp")
self.file_path=fileName
ifself.file_path!="":
head, tail=os.path.split(fileName)
self.line_edit.setText(tail)
self.label.hide()
self.label2.hide()
self.label3.hide()
self.label4.hide()
self.label_1.hide()
self.label_2.hide()
self.label_1.move(410, 125)
self.label_1.setText('and please wait..')
self.label_1.setFont(QtGui.QFont("Sanserif", 12))
self.label_2.move(410, 100)
self.label_2.setText('click test')
self.label_2.setFont(QtGui.QFont("Sanserif", 12))
pixmap=QPixmap(self.file_path)
self.label.setPixmap(pixmap)
self.label.resize(190, 190)
self.label.move(205, 95)
self.label.setPixmap(pixmap.scaled(self.label.size(), Qt.IgnoreAspectRatio))
# image information
image=Image.open(self.file_path)
width, height=image.size
resolution="Resolution "+str(width) +"X"+str(height)
self.label2.setText(resolution)
self.label2.setFont(QtGui.QFont("Sanserif", 8))
self.label2.move(15, 100)
head, tail=os.path.split(self.file_path)
tail2=tail.split('.')[1]
file_type="Item Type "+str(tail2)
self.label3.setText(file_type)
self.label3.setFont(QtGui.QFont("Sanserif", 8))
self.label3.move(15, 112)
size=os.path.getsize(self.file_path)
size=np.int(size/1000)
text=str(size) +"KB"
self.label4.setText(text)
self.label4.setFont(QtGui.QFont("Sanserif", 8))
self.label4.move(15, 124)
self.label2.show()
self.label3.show()
self.label4.show()
self.label.show()
self.label_1.show()
self.label_2.show()
else:
self.file_path=fileName
head, tail=os.path.split(fileName)
self.line_edit.setText(tail)
self.label.hide()
self.label2.hide()
self.label3.hide()
self.label4.hide()
self.label_1.move(410, 125)
self.label_1.setText('and please wait..')
self.label_1.setFont(QtGui.QFont("Sanserif", 12))
self.label_2.move(410, 100)
self.label_2.setText('click test')
self.label_2.setFont(QtGui.QFont("Sanserif", 12))
pixmap=QPixmap(self.file_path)
self.label.setPixmap(pixmap)
self.label.resize(190, 190)
self.label.move(205, 95)
self.label.setPixmap(pixmap.scaled(self.label.size(), Qt.IgnoreAspectRatio))
# image information
image=Image.open(self.file_path)
width, height=image.size
resolution="Resolution "+str(width) +"X"+str(height)
self.label2.setText(resolution)
self.label2.setFont(QtGui.QFont("Sanserif", 8))
self.label2.move(15, 100)
head, tail=os.path.split(self.file_path)
tail2=tail.split('.')[1]
file_type="Item Type "+str(tail2)
self.label3.setText(file_type)
self.label3.setFont(QtGui.QFont("Sanserif", 8))
self.label3.move(15, 112)
size=os.path.getsize(self.file_path)
size=np.int(size/1000)
text=str(size) +"KB"
self.label4.setText(text)
self.label4.setFont(QtGui.QFont("Sanserif", 8))
self.label4.move(15, 124)
self.label_1.show()
self.label_2.show()
self.label2.show()
self.label3.show()
self.label4.show()
@pyqtSlot()
defon_click(self):
ifself.file_path=="":
msg=QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Choose image from your computer !")
msg.setWindowTitle("Error")
msg.setWindowIcon(QtGui.QIcon("icons8-cbs-512.ico"))
msg.exec_()
else:
file_name=self.model_name
name_1=file_name.split('.')[0]
print("*"*50)
print("Test_with_Retraind_window")
print(name_1)
name_2=name_1.split('_')[0]
print(name_2)
print("*"*50)
ifstr(name_2) =="ELA":
model=self.model_path
self.myThread=Thread()
label, prob=self.myThread.test_image_with_ela(self.file_path, model_path=model)
self.myThread.start()
self.close()
self.result_window=ResultWindow(label, prob , model_path=self.model_path ,model_name=name_2)
self.result_window.show()
ifstr(name_2) =="VGG16":
model=self.model_path
self.myThread=Thread()
label, prob=self.myThread.test_image_with_vgg16(self.file_path, model_path=model)
self.myThread.start()
self.close()
self.result_window=ResultWindow(label, prob , model_path=self.model_path ,model_name=name_2)
self.result_window.show()
ifstr(self.combo.currentText()) =="VGG19":
model=self.model_path
self.myThread=Thread()
label, prob=self.myThread.test_image_with_vgg19(self.file_path, model_path=model)
self.myThread.start()
self.close()
self.result_window=ResultWindow(label, prob , model_path=self.model_path ,model_name=name_2)
self.result_window.show()
@pyqtSlot()
defclosex(self):
reply=QMessageBox.question(self, "Quit", "Are you sure you want to quit?",
QMessageBox.Cancel|QMessageBox.Close)
ifreply==QMessageBox.Yes:
self.close()
@pyqtSlot()
defkeyPressEvent(self, event):
"""Close application from escape key.
results in QMessageBox dialog from closeEvent, good but how/why?
"""
ifevent.key() ==Qt.Key_Escape:
reply=QMessageBox.question(
self, "Message",
"Are you sure you want to quit?",
QMessageBox.Close|QMessageBox.Cancel)
ifreply==QMessageBox.Close:
self.close()
@pyqtSlot()
defclose_main_window(self):
"""
Generate 'question' dialog on clicking 'X' button in title bar.
Reimplement the closeEvent() event handler to include a 'Question'
dialog with options on how to proceed - Save, Close, Cancel buttons
"""
reply=QMessageBox.question(self, "Quit", "Are you sure you want to quit?",
QMessageBox.Cancel|QMessageBox.Close)
ifreply==QMessageBox.Close:
self.close()
if__name__=="__main__":
App=QApplication(sys.argv)
App.setStyle('Fusion')
window=Test_window()
sys.exit(App.exec())